<?php
/**
 *   Joomdiet - components/models/food.php
 *   Author: Juan Dapena Paz (juan@bittingbits.com)
 *   Copyright (C) 2010 Bitting Bits S.L.
 *
 *   This program is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
defined('_JEXEC') or die('Restricted access');
jimport('joomla.application.component.model');

class JoomdietModelFood extends JModel
{
    var $_data; // result data
    var $_query; // generated query

    function __construct()
    {
        parent::__construct();
    }
    
    function _buildBaseQuery()
    {
        $this->_query = 'SELECT f.* FROM #__joomdiet_Food f ';
        $this->_query .= ' LEFT JOIN #__joomdiet_Food_Category fc ON f.food_category_id = fc.id';
        $this->_query .= ' LEFT JOIN #__joomdiet_Food_variable fv ON f.id = fv.id_food';
        return $this->_query;
    }
    
    function _buildWhereStatement($filter='', $category=0, $subcategory=0, 
                                  $composition='')
    {
        // name filter
        $this->_query .= ' WHERE 1=1';
        if ($filter != '') {
            $filter_words = explode(' ', $filter);
            foreach ($filter_words as $word) {
                $this->_query .= ' AND f.name LIKE "%'.$filter.'%"';
            }
        }
        
        // category filter
        if ($category != 0) {
            $this->_query .= ' AND (f.food_category_id='.$category;
            $this->_query .= ' OR fc.parent_category='.$category.')';
        }
        
        // subcategory filter
        if ($category != 0 and $subcategory != 0) {
            $this->_query .= ' AND f.food_category='.$subcategory;
        }
        
        // composition filter
        if ($composition != 0) {
            $this->_query .= ' AND fv.id_variable='.$composition;
        }
    }
    
    function _buildGroupAndOrder($order = '')
    {
        $this->_query .= ' GROUP BY f.id';
        if ($order != '') {
            $this->_query .= ' ORDER BY fv.amount '.$order;
        }
    }
    
    function getData($filter='', $category=0, $subcategory=0, $composition='', $order='')
    {
        if (empty($this->_data)) {
            $this->_buildBaseQuery();
            $this->_buildWhereStatement($filter, $category, $subcategory, $composition);
            $this->_buildGroupAndOrder($order);
            
            $this->_db->setQuery($this->_query);
            $this->_data = $this->_getList($this->_query);
        }
        
        return $this->_data;
    }
    
    function getModel()
    {
        return $this;
    }
    
    function getFood($id)
    {
        $query = 'SELECT * FROM #__joomdiet_Food WHERE id='.$id;
        $this->_db->setQuery($query);
        $object = $this->_db->loadObject();
        return $object;
    }
}
?>
